package AEF::AEFDump;

use Modern::Perl;
use Moose;
use Log::Log4perl qw( :easy );

require AEF::AEFProcessor;

extends 'AEF::AEFProcessor';

override 'RequireDataForType' => sub
{
	my $self = shift;
	my $type = shift;

	return 1;
};

override 'ProcessBlock' => sub 
{
	my $self = shift;
	my ($type, $data_chunk, $ptr) = @_;
	my $logger = get_logger();
	if(ref $data_chunk eq "ARRAY")
	{
		$logger->info(sprintf "Type [0x%x] %d", $type, $type);
		$logger->info($self->hexDump($data_chunk));
	}
	else
	{
		$logger->info(sprintf "Type [0x%x] %d ->  %s", $type, $type, $data_chunk);
	}
};

sub hexDump
{
	my $self = shift;
	my $data = shift;
	my $length = @$data;
	my $string = "";
	for (my $i = 0; $i < $length; $i+=16)
	{
		$string .= "\n" if($i > 0);
		my @chars = splice(@$data, 0, 16);
		my $char_length = @chars;
		my @strings = map { sprintf "%2.2X ", $_ } @chars;
		my @chars_strings = map { $_ >= 32 && $_ < 128 ? sprintf "%c", $_ : '.' } @chars;
		my $hex_string = join "", @strings;
		$string .= $hex_string . ' ' x (48 - length($hex_string));
		$string .= " " . join "", @chars_strings;
	}
	return $string
}

override 'TypeMap' => sub
{
	my $self = shift;
	my $type = shift;

	my $format = $self->typeFormats->{$type} || 'C*';
	return $format;
};

1;

no Moose;

